<template>
  <div>
    <el-form :inline="true" :model="q" size="mini">
      <el-form-item label="模版类型：">
        <el-select v-model="q.temp_type" clearable placeholder="请选择">
          <el-option v-for="item in temp_type" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="问题类型：">
        <el-select v-model="q.type_id" clearable placeholder="请选择">
          <el-option v-for="item in problemsTypeList" :key="item.id" :label="item.desc" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="query">查询</el-button>
        <el-button type="danger" @click="reload">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row type="flex" class="row-bg btn-row" justify="end">
      <el-button size="mini" @click="add" type="primary" icon="el-icon-plus">新增常用语
      </el-button>
    </el-row>
    <el-table class="el-calendar-table" :data="listData.items" v-loading.body="listLoading" size="mini" stripe border
      fit highlight-current-row>
      <el-table-column prop="id" label="ID" width="80">
      </el-table-column>
      <el-table-column prop="temp_type" label="模版类型" width="80">
        <template slot-scope="scope">
          {{ $store.state.app.temp_type[scope.row.temp_type] }}
        </template>
      </el-table-column>
      <el-table-column prop="title" label="模版名称" width="150">
      </el-table-column>
      <el-table-column prop="context" label="模板内容" min-width="280">
        <template slot-scope="scope"><div class="ellipsis">{{scope.row.context}}</div></template>
      </el-table-column>
      <el-table-column width="120" prop="type.desc" label="关联问题类型">
      </el-table-column>
      <el-table-column width="210" prop="user_name" label="操作人">
      </el-table-column>
      <el-table-column min-width="150" prop="created_at" label="创建时间">
      </el-table-column>
      <el-table-column min-width="150" prop="updated_at" label="更新时间">
      </el-table-column>
      <el-table-column label="操作" width="130" fixed="right">
        <template slot-scope="scope">
          <div>
            <el-button v-if="!(scope.row.temp_type===2&&$store.state.user.userinfo.role_id===1)" type="primary" size="mini" @click="update(scope.row)">编辑</el-button>
            <el-button v-if="!(scope.row.temp_type===2&&$store.state.user.userinfo.role_id===1)" type="success" size="mini" @click="deleteRow(scope.row)">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="q.page_no"
        :page-sizes="$store.state.app.pageSizeArr" :page-size="q.page_size"
        layout="total, sizes, prev, pager, next, jumper" :total="listData.total">
      </el-pagination>
    </div>
    <save-update v-if="showUpdate" :showUpdate.sync="showUpdate" :id="show_id" :problemsTypeList="problemsTypeList">
    </save-update>
  </div>
</template>

<script>
import { getCommonStatement, getProblemsList, deleteCommonStatement } from '@/api'
import saveUpdate from './saveUpdate.vue'
import { queryFilter } from "@/utils/patch";

export default {
  name: "index",
  components: { saveUpdate },
  data () {
    return {
      show_id: null,
      listData: {},
      q: { page_size: 30, page_no: 1 },
      listLoading: false,
      showUpdate: false,
      problemsTypeList: [],
      temp_type:[{
        label: '公用模版',
        value: 2
      },{
        label: '个人模版',
        value: 1
      }]
    }
  },
  created () {
    this.listLoading = true
    this.getProblemsList()
    this.getList()
    console.log(this.$store.state.user.userinfo.role_id, 'role_id')
  },
  methods: {
    add () {
      this.show_id = null
      this.showUpdate = true
    },
    update (row) {
      this.show_id = row.id
      this.showUpdate = true
    },
    deleteRow (row) {
      this.$confirm(`确定删除【${row.id}】吗？`, '是否继续', {
        type: 'warning',
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(() => {
        deleteCommonStatement(row.id).then(res => {
          if (res.status === 0) {
            this.$message({ type: 'success', message: '删除成功' })
            this.getList()
          }
        })
      }).catch(() => {
        this.$message({ type: 'info', message: '已取消' })
      })
    },
    query () {
      this.getList()
    },
    reload () {
      this.q = { page_size: 30, page_no: 1 }
      this.getList()
    },
    handleSizeChange (val) {
      this.q.page_size = val
      this.getList()
    },
    handleCurrentChange (val) {
      console.log(val)
      this.q.page_no = val
      this.getList()
    },

    getList () {
      let queryObj = queryFilter(this.q)
      this.listLoading = true
      getCommonStatement(queryObj).then(res => {
        console.log(res, 'getList')
        if (res.status === 0) {
          this.listLoading = false
          this.listData = res.data
        }
      })
    },
    getProblemsList () {
      getProblemsList().then(res => {
        if (res.status === 0) {
          this.problemsTypeList = res.data.items
        }
      })
    }
  },
  watch: {
    showUpdate () {
      if(this.showUpdate!==0&&!this.showUpdate){
        this.getList()
      }
    }
  }
}
</script>

<style scoped>
</style>
